using System;
using System.Web.UI.WebControls;
using FAQComponents.Objects;
using FreeTextBoxControls;
using System.Web.UI;

namespace Adge.UI.WebControls
{
	/// <summary>
	/// Summary description for AdminPage.
	/// </summary>
	public class ClientSidePage : System.Web.UI.Page
	{
		#region Methods for Displaying an Alert
		protected void DisplayClientSideAlert(string message)
		{
			Page.RegisterStartupScript(System.Guid.NewGuid().ToString(), 
				"<script language=\"JavaScript\">" + GetClientSideAlertScript(message) + "</script>");
		}

		protected string GetClientSideAlertScript(string message)
		{
			return "alert('" + EscapeJavaScript(message) + "');";
		}
		#endregion

		#region Methods for Prompting the User to Save Before Leaving the Page
		protected void MonitorChanges(FreeTextBox ftb)
		{
			if (ftb == null) return;

			Page.RegisterArrayDeclaration("monitorChangesIDs", string.Concat("\"", ftb.ClientID, "\""));
			Page.RegisterArrayDeclaration("monitorChangesValues", "null");

			AssignMonitorChangeValuesOnPageLoad();
		}

		protected void MonitorChanges(WebControl wc)
		{
			if (wc == null) return;

			if (wc is CheckBoxList || wc is RadioButtonList)
				for (int i = 0; i < ((ListControl) wc).Items.Count; i++)
				{
					Page.RegisterArrayDeclaration("monitorChangesIDs", "\"" + string.Concat(wc.ClientID, "_", i.ToString()) + "\"");
					Page.RegisterArrayDeclaration("monitorChangesValues", "null");
				}
			else
			{
				Page.RegisterArrayDeclaration("monitorChangesIDs", string.Concat("\"", wc.ClientID, "\""));
				Page.RegisterArrayDeclaration("monitorChangesValues", "null");
			}			

			AssignMonitorChangeValuesOnPageLoad();
		}

		protected void AssignMonitorChangeValuesOnPageLoad()
		{
			if (!Page.IsStartupScriptRegistered("monitorChangesAssignment"))
			{
				Page.RegisterStartupScript("monitorChangesAssignment",
					@"<script language=""JavaScript"">
						assignInitialValuesForMonitorChanges();
					  </script>");

				Page.RegisterClientScriptBlock("monitorChangesAssignmentFunction",
					@"<script language=""JavaScript"">
						function assignInitialValuesForMonitorChanges() {
						    for (var i = 0; i < monitorChangesIDs.length; i++) {
						      var elem = document.getElementById(monitorChangesIDs[i]);
						      if (elem) if (elem.type == 'checkbox' || elem.type == 'radio') 
						          monitorChangesValues[i] = elem.checked;
						      else monitorChangesValues[i] = elem.value;
						    }
						 }
               
						var needToConfirm = true;
						window.onbeforeunload = confirmClose;
						
						function confirmClose() {
							if (!needToConfirm) return;

							for (var i = 0; i < monitorChangesValues.length; i++) {
								var elem = document.getElementById(monitorChangesIDs[i]);
								if (elem) if (((elem.type == 'checkbox' || elem.type == 'radio') && elem.checked != monitorChangesValues[i]) || (elem.type != 'checkbox' && elem.type != 'radio' && elem.value.toLowerCase() != monitorChangesValues[i].toLowerCase())) { needToConfirm = false; setTimeout('resetFlag()', 750); return ""You have modified the data entry fields since last savings. If you leave this page, any changes will be lost. To save these changes, click Cancel to return to the page, and then Save the data.""; }
							}
						}
               
						function resetFlag() { needToConfirm = true; }
					</script>");
			}
		}

		protected void BypassModifiedMethod(WebControl wc)
		{
			wc.Attributes["onclick"] = string.Concat("javascript:", GetBypassModifiedMethodScript());
		}

		protected string GetBypassModifiedMethodScript()
		{
			return "needToConfirm = false;";
		}
		#endregion

		protected virtual string EscapeJavaScript(string message)
		{
			return message.Replace("'", "\\'").Replace("\"", "\\\"").Replace(System.Environment.NewLine, "\n");
		}

        void PrintAllControlID(Control ctrl)
        {
            if (ctrl.HasControls())
            {
                foreach (Control subctrl in ctrl.Controls)
                {
                    PrintAllControlID(subctrl);
                }
            }
            
            if (!string.IsNullOrEmpty(ctrl.UniqueID) && !string.IsNullOrEmpty(ctrl.ID))
            {
                Response.Write(ctrl.ID);
                Response.Write(ctrl.UniqueID);
                Response.Write(ctrl.GetType().FullName);
            }
            if (ctrl is TextBox)
            {
                Response.Write(((TextBox)ctrl).Text);
            }
        }
	}
}
